package course.p6_queue;

/**
 * 链表实现队列
 */
public class LinkQueue implements Queue{


    //队列头部
    Node head;
    // 队列尾部
    Node tail;
    // 队列元素数量
    int size;
    // 队列容量
    int capacity;

    static class Node {
        int data;
        Node next;
        Node(int data){
            this.data = data;
        }
    }

    public LinkQueue() {
        this.capacity = 16;
        this.size = 0;
    }

    public LinkQueue(int capacity){
        this.capacity = capacity;
        this.size = 0;
    }


    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean isFull() {
        return size == capacity;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void offer(int data) {
        if(head==null){
            head = new Node(data);
            tail = head;
            size++;
        }
        else{
            if(isFull()){
                System.out.println("queue is Full!!");
            }else{
                tail.next = new Node(data);
                tail = tail.next;
                size++;
            }
        }
    }

    @Override
    public int poll() {
        int res  = -1;
        if(isEmpty()) {
            System.out.println("queue is empty!!");
        }
        else{
            res = head.data;
            head = head.next;
            size--;
        }
        return res;
    }

}
